<template>
  <!-- 权益列表数据 -->
  <el-button type="primary" plain @click="dialogAdd">添加自定义权益</el-button>
  <el-table :data="equity" style="width: 100%">
    <el-table-column prop="EName" label="活动名称"></el-table-column>
    <el-table-column prop="EDescription" label="活动描述"></el-table-column>
    <el-table-column prop="EStatus" label="状态">
      <template #default="scope">
        <el-tag :type="scope.row.EStatus ? 'success' : 'danger'">{{
          scope.row.EStatus ? "生效" : "已失效"
        }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="120">
      <template v-slot="scope">
        <el-button
          link
          type="primary"
          size="small"
          @click="SkipDelital(scope.row.EId)"
          >查看</el-button
        >
        <el-button
          link
          type="primary"
          size="small"
          @click="DelEquity(scope.row.EId)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>

  <!-- 权益设置弹窗 -->
  <el-dialog v-model="dialogFormVisible" title="权益设置" width="500">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="form"
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="权益名称" prop="EName">
        <el-input v-model="form.EName" />
      </el-form-item>

      <el-form-item label="权益状态">
        <el-switch v-model="form.EStatus" />
      </el-form-item>

      <el-form-item label="权益说明" prop="EDescription">
        <el-input v-model="form.EDescription" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="AddEquity(ruleFormRef)">
          确 认
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
// 引入组件
import { ElTable } from "element-plus";
import { reactive, ref, onMounted } from "vue";
//引入路由
import { useRouter, useRoute } from "vue-router";
import axios from "axios";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
const ruleFormRef = ref<FormInstance>();

axios.defaults.baseURL = "http://localhost:5089/api/";

const dialogFormVisible = ref(false);
//路由变量
const router = useRouter();

//当前页面路由
const route = useRoute();

const equity = ref([]);

//权益数据
const form = reactive({
  EId: 0,
  EName: "",
  EDescription: "",
  EStatus: true,
  EIcon: "string",
  TenantId: "default",
});

//权益校验
const rules = reactive<FormRules>({
  EName: [
    { required: true, message: "请输入权益名称", trigger: "blur" },
    { min: 2, max: 20, message: "权益名称长度介于2-20之间", trigger: "blur" },
  ],
  EDescription: [
    { required: true, message: "请输入权益说明", trigger: "blur" },
    { min: 2, max: 30, message: "权益说明长度介于2-20之间", trigger: "blur" },
  ],
});

// 权益列表数据
function GetEquity() {
  axios.get("Equities/GetEquitiesList").then((res) => {
    equity.value = res.data;
  });
}

// 添加自定义权益
const AddEquity = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      axios.post("Equities/EquitiesAdd", form).then((res) => {
        if (res.data.Code == 200) {
          ElMessage.success("添加成功！");
          dialogFormVisible.value = false;
          GetEquity();
        } else {
          ElMessage.error("添加失败或者已有相同权益服务！");
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

// 取消
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

// 删除权益
const DelEquity = (EId) => {
  ElMessageBox.confirm("确认删除房号?", "房号删除", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      axios.delete("Equities/DeleteEquities?eId=" + EId).then((res) => {
        if (res.data.Code == 200) {
          ElMessage.success("删除成功");
          GetEquity();
        } else {
          ElMessage.error("删除失败");
          return;
        }
      });
    })
    .catch(() => {});
};

const dialogAdd = () => {
  dialogFormVisible.value = true;
};

//跳转到详情
const SkipDelital = (EId: number) => {
  router.push("EquityDelital?eId=" + EId);
};

//钩子函数
onMounted(() => {
  GetEquity();
});
</script>

<style scoped></style>
